import {defineStore} from "pinia";
import {reactive} from "vue";
import type User from "@/models/user";

const  userStore = defineStore('user', ()=> {
    let json:string|null = localStorage.getItem("user");
    let user:User;
    if (json){
        user = JSON.parse(json);
    }else{
        user = {
            userId: '',
            username: '',
            token: '',
            expireAt: null,
        }
    }
    const userReactive = reactive(user);
    const save = (user: User) => {
        localStorage.setItem('user', JSON.stringify(user));
        userReactive.userId = user.userId;
        userReactive.token = user.token;
        userReactive.expireAt = user.expireAt;
        userReactive.username = user.username;
    };
    return {user: userReactive, save}
});